<?php
/**
 * Created by PhpStorm.
 * User: 江雪
 * Date: 2019/4/29 0029
 * Time: 21:19
 */

namespace app\index\controller;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Timer extends Command
{
    protected function configure()
    {
        $this->setName('timer')
            ->setDescription('timer');
    }

    protected function execute(Input $input, Output $output)
    {
        $serv = new \swoole_server("0.0.0.0",9502);
        $serv->on("WorkerStart",function ($serv,$woker_id){
            if ($woker_id == 0){
                //每隔1000ms触发一次
                swoole_timer_tick(1000, function ($timer_id) {
                    //写入数据操作
                    Db::name('test')->insert(['content'=>rand(100,999)]);
                    echo "我是定时器\n";
                });
            }
        });

        $serv->on("receive",function ($serv,$fd,$from_id,$data){
            $serv->send($fd,"Server:".$data);
        });

        $serv->start();
    }
}